<template>
  <div class="login-bg-new">
    <div class="login-center">
      <div class="login-card-new">
        <div class="login-tabs-new">
          <button :class="{active: tab==='login'}" @click="tab='login'">登录</button>
          <button :class="{active: tab==='register'}" @click="$emit('to-register')">注册</button>
        </div>

        <el-form :model="form" :rules="rules" ref="loginForm" @keyup.enter="onLogin" status-icon>
          <el-form-item label="用户名" prop="username">
            <el-input v-model="form.username" placeholder="请输入用户名" clearable />
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="form.password" :type="showPwd ? 'text' : 'password'" placeholder="请输入密码" clearable>
              <template #suffix>
                <el-icon @click="showPwd = !showPwd" style="cursor:pointer;">
                  <View v-if="!showPwd" />
                  <Hide v-else />
                </el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item label="验证码">
            <el-input v-model="form.captcha" placeholder="请输入验证码" style="width: 120px; margin-right: 10px;" />
            <img :src="captchaImg" @click="getCaptcha" style="cursor:pointer; vertical-align: middle;" />
          </el-form-item>
          <el-form-item>
            <el-checkbox v-model="form.remember">记住我</el-checkbox>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" :loading="loading" @click="onLogin" style="width: 100%;">登录</el-button>
          </el-form-item>
        </el-form>
        <el-alert v-if="msg" :title="msg" :type="msgType" show-icon class="login-alert" />
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import axios from 'axios'
import { View, Hide } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

const emit = defineEmits(['to-register', 'to-lottery', 'to-wins'])

const tab = ref('login')
const form = ref({
  username: '',
  password: '',
  remember: false,
  captcha: '',
  captchaUuid: ''
})
const rules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
}
const loginForm = ref()
const msg = ref('')
const msgType = ref('info')
const loading = ref(false)
const showPwd = ref(false)
const captchaImg = ref('')

const getCaptcha = async () => {
  const res = await axios.get('/api/captcha')
  captchaImg.value = res.data.img
  form.value.captchaUuid = res.data.uuid
  form.value.captcha = ''
}
onMounted(getCaptcha)

const onLogin = async () => {
  await loginForm.value.validate(async valid => {
    if (!valid) return
    loading.value = true
    msg.value = ''
    try {
      const res = await axios.post('/api/user/login', {
        username: form.value.username,
        password: form.value.password,
        captcha: form.value.captcha,
        captchaUuid: form.value.captchaUuid
      })
      msg.value = res.data.message
      msgType.value = res.data.success ? 'success' : 'error'
      if (res.data.success) {
        if (form.value.remember) {
          localStorage.setItem('username', form.value.username)
        } else {
          localStorage.removeItem('username')
        }
        setTimeout(() => {
          emit('to-lottery')
        }, 1000)
      }
    } catch (e) {
      msg.value = '登录失败'
      msgType.value = 'error'
      getCaptcha() // 刷新验证码
    } finally {
      loading.value = false
    }
  })
}

if (localStorage.getItem('username')) {
  form.value.username = localStorage.getItem('username')
  form.value.remember = true
}
</script>

<style>
body {
  background: #000 !important;
}
</style>

<style scoped>
@import url('https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css');

.login-bg-new {
  min-height: 100vh;
  background: url('../../anheng.png') no-repeat center center;
  background-size: cover;
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-center {
  width: 100vw;
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-card-new {
  background: rgba(40, 42, 48, 0.9);
  border-radius: 24px;
  box-shadow: 0 12px 48px rgba(0,0,0,0.5);
  padding: 40px 32px 32px 32px;
  width: 400px;
  max-width: 95vw;
  backdrop-filter: blur(10px);
  border: 1.5px solid #3a3c42;
}

.login-tabs-new {
  display: flex;
  justify-content: center;
  margin-bottom: 28px;
  gap: 12px;
}

.login-tabs-new button {
  background: none;
  border: none;
  color: #ddd;
  font-size: 18px;
  padding: 10px 35px;
  border-radius: 25px;
  cursor: pointer;
  font-weight: bold;
  transition: all 0.3s ease;
}

.login-tabs-new .active {
  background: linear-gradient(90deg, #c81e2b 0%, #d9534f 100%);
  color: #fff;
  box-shadow: 0 0 20px #d9534f77;
}

.login-icons-new {
  display: flex;
  justify-content: center;
  gap: 35px;
  margin-bottom: 28px;
  font-size: 30px;
}

.login-icons-new .el-icon {
  color: #c81e2b;
  background: #2a2d32;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 4px 12px #000000aa;
  transition: all 0.3s ease;
}

.login-icons-new .el-icon:hover {
  background: #d9534f;
  color: #fff;
  transform: translateY(-3px);
}

.login-title {
  display: none;
}

.el-form {
  margin-top: 12px;
}

.el-form-item__label {
  color: #c0c4cc;
}

.el-input__wrapper {
  background: #2a2d32 !important;
  border-radius: 10px;
  color: #fff;
  border: 1px solid #444;
}

.el-input__inner {
  color: #fff;
}

.el-checkbox__label {
  color: #c0c4cc;
}

.el-button--primary {
  background: linear-gradient(90deg, #c81e2b 0%, #d9534f 100%);
  border: none;
  font-weight: bold;
  border-radius: 10px;
  padding: 18px 0;
  font-size: 16px;
}

.login-alert {
  margin-top: 20px;
}

.login-link {
  margin-top: 22px;
  text-align: center;
  color: #c0c4cc;
  font-size: 15px;
}
</style>